import axios from 'axios'

// 创建 axios 实例
const request = axios.create({
    baseURL: '/api',
    timeout: 10000,
    headers: {
        'Content-Type': 'application/json'
    }
})

// 请求拦截器
request.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token')
        if (token) {
            config.headers.Authorization = `Bearer ${token}`
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// 响应拦截器
request.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        return Promise.reject(error)
    }
)

// 获取所有公告
export function listNotices() {
    return request({
        url: '/notice/list',
        method: 'get'
    })
}

// 根据ID获取公告
export function getNotice(id) {
    return request({
        url: `/notice/${id}`,
        method: 'get'
    })
}

// 添加新公告
export function addNotice(data) {
    return request({
        url: '/notice/add',
        method: 'post',
        data
    })
}

// 更新公告
export function updateNotice(data) {
    return request({
        url: '/notice/update',
        method: 'put',
        data
    })
}

// 删除公告
export function deleteNotice(id) {
    return request({
        url: `/notice/delete/${id}`,
        method: 'delete'
    })
}

// 获取置顶公告
export function listTopNotices() {
    return request({
        url: '/notice/top',
        method: 'get'
    })
}

export default request
